package com.tledu.ssm.service.impl;

import com.tledu.ssm.core.exception.LoginException;
import com.tledu.ssm.mapper.UserMapper;
import com.tledu.ssm.model.User;
import com.tledu.ssm.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author cyrus
 */
@Service
@AllArgsConstructor
public class UserServiceImpl implements IUserService {

    private final UserMapper userMapper;

    @Override
    public List<User> list(User user) {
        return userMapper.list(user);
    }

    @Override
    public User login(String username, String password) throws LoginException {
        User user = userMapper.selectByUsername(username);
        if (user == null) {
            throw new LoginException("用户不存在");
        }
        // 这个用户存在了
        if (!password.equals(user.getPassword())) {
            throw new LoginException("密码错误");
        }
        // 代表用户存在密码正确
        return user;
    }
}
